﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
using System.IO;

namespace Vincent
{
    /// <summary>
    /// 文件上传
    /// </summary>
    public class _UpLoad
    {                
        /// <summary>
        /// 文件上传方法A
        /// </summary>
        /// <param name="postedFile">文件流</param>
        /// <param name="isThumbnail">是否生成缩略图</param>
        /// <param name="isWater">是否打水印</param>
        /// <returns>服务器文件路径</returns>
        public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater)
        {
            return fileSaveAs(postedFile, isThumbnail, isWater, false);
        }

        /// <summary>
        /// 文件上传方法B
        /// </summary>
        /// <param name="postedFile">文件流</param>
        /// <param name="isThumbnail">是否生成缩略图</param>
        /// <param name="isWater">是否打水印</param>
        /// <param name="_isReOriginal">是否返回文件原名称</param>        
        /// <returns>服务器文件路径</returns>
        public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, bool _isReOriginal)
        {
            return fileSaveAs(postedFile, isThumbnail, isWater, _isReOriginal, "");
        }

        /// <summary>
        /// 文件上传方法B
        /// </summary>
        /// <param name="postedFile">文件流</param>
        /// <param name="isThumbnail">是否生成缩略图</param>
        /// <param name="isWater">是否打水印</param>
        /// <param name="_isReOriginal">是否返回文件原名称</param>
        /// <param name="fileName">不包含扩展名</param>
        /// <returns>服务器文件路径</returns>
        public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, bool _isReOriginal, string fileName)
        {
            try
            {
                string fileExt = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(".") + 1); //文件扩展名，不含“.”
                string originalFileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得文件原名
                
                if(fileName == "")
                    fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") +"." + fileExt; //随机文件名
                else
                    fileName = fileName + "." + fileExt; //随机文件名

                string dirPath = GetUpLoadPath(); //上传目录相对路径

                //检查文件扩展名是否合法
                if (!CheckFileExt(fileExt))
                {
                    return "{msg: 0, msbox: \"不允许上传" + fileExt + "类型的文件！\"}";
                }
                //检查文件大小是否合法
                if (!CheckFileSize(fileExt, postedFile.ContentLength))
                {
                    return "{msg: 0, msbox: \"文件超过限制的大小啦！\"}";
                }
                //获得要保存的文件路径
                string serverFileName = dirPath + fileName;
                string serverThumbnailFileName = dirPath + "small_" + fileName;
                string returnFileName = serverFileName;
                //物理完整路径                    
                string toFileFullPath = dirPath;
                //检查有该路径是否就创建
                if (!Directory.Exists(toFileFullPath))
                {
                    Directory.CreateDirectory(toFileFullPath);
                }
                //保存文件
                postedFile.SaveAs(toFileFullPath + fileName);
                
                //如果是图片，检查图片尺寸是否超出限制
                //Thumbnail.MakeThumbnailImage(toFileFullPath + fileName, toFileFullPath + fileName, this.siteConfig.attachimgmaxwidth, this.siteConfig.attachimgmaxheight);
                
                //是否生成缩略图
                //if (IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0)
                //{
                //    Thumbnail.MakeThumbnailImage(toFileFullPath + fileName, toFileFullPath + "small_" + fileName, this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, "Cut");
                //    returnFileName += "," + serverThumbnailFileName; //返回缩略图，以逗号分隔开
                //}

                ////是否打图片水印
                //if (IsWaterMark(fileExt) && isWater)
                //{
                //    WaterMark.AddImageSignText(serverFileName, serverFileName,
                //        this.siteConfig.watermarktext, this.siteConfig.watermarkposition,
                //        this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize);
                //}

                //如果需要返回原文件名
                if (_isReOriginal)
                {
                    return "{msg: 1, msbox: \"" + serverFileName + "\", mstitle: \"" + originalFileName + "\"}";
                }
                return "{msg: 1, msbox: \"" + returnFileName + "\"}";
            }
            catch
            {
                return "{msg: 0, msbox: \"上传过程中发生意外错误！\"}";
            }
        }

        #region 私有方法

        /// <summary>
        /// 返回上传目录相对路径
        /// </summary>
        /// <param name="fileName">上传文件名</param>
        private string GetUpLoadPath()
        {
            return _Utility.GetWebApplicationRootPath() + "upload\\"; //站点目录+上传目录            
        }

        /// <summary>
        /// 是否需要打水印
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        private bool IsWaterMark(string _fileExt)
        {
            //判断是否可以打水印的图片类型
            ArrayList al = new ArrayList();
            al.Add("bmp");
            al.Add("jpeg");
            al.Add("jpg");
            al.Add("png");
            if (al.Contains(_fileExt.ToLower()))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 是否为图片文件
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        private bool IsImage(string _fileExt)
        {
            ArrayList al = new ArrayList();
            al.Add("bmp");
            al.Add("jpeg");
            al.Add("jpg");
            al.Add("gif");
            al.Add("png");
            if (al.Contains(_fileExt.ToLower()))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 检查是否为合法的上传文件
        /// </summary>
        private bool CheckFileExt(string _fileExt)
        {
            string[] allowExt = new string[4] { "bmp", "jpeg", "jpg", "gif" };
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i].ToLower() == _fileExt.ToLower()) { return true; }
            }
            return false;
        }

        /// <summary>
        /// 检查文件大小是否合法
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        /// <param name="_fileSize">文件大小(KB)</param>
        private bool CheckFileSize(string _fileExt, int _fileSize)
        {
            //判断是否为图片文件
            //if (IsImage(_fileExt))
            //{
            //    if (_fileSize > 2 * 1024)
            //    {
            //        return false;
            //    }
            //}
            //else
            //{
            //    if (_fileSize > 2 * 1024)
            //    {
            //        return false;
            //    }
            //}
            return true;
        }

        #endregion
       
    }
}
